package com.fz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.TUser;
import com.fz.service.TUserService;
import com.fz.util.JwtUtil;
import com.fz.vo.LoginVO;
import com.fz.vo.ResultVo;
import io.minio.errors.*;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author fz
 * @since 2024-08-15
 */
@RestController
@RequestMapping("/tUser")
public class TUserController {
    @Resource
    TUserService tUserService;
    @Resource
    PasswordEncoder passwordEncoder;

    @GetMapping("/Adminlist")
    public ResultVo<IPage<TUser>> Adminlist(Integer PageNo, Integer PageSize) throws InterruptedException {
        Page<TUser> page = new Page<>(PageNo, PageSize);
        IPage<TUser> resultPage = tUserService.page(page, new QueryWrapper<TUser>().in("role", "[1]", "[3]"));
        return ResultVo.success(resultPage);
    }
    @GetMapping("/Userlist")
    public ResultVo<IPage<TUser>> Userlist(Integer PageNo, Integer PageSize) throws InterruptedException {
        Page<TUser> page = new Page<>(PageNo, PageSize);
        IPage<TUser> resultPage = tUserService.page(page, new QueryWrapper<TUser>().in("role", "[2]"));
        return ResultVo.success(resultPage);
    }
    @PostMapping("/login")
    public ResultVo<String> login(String phone, String password) {
        return ResultVo.success(JwtUtil.generateToken(tUserService.login(phone, password)));
    }
    @PostMapping("/register")
    public ResultVo<Boolean> register(String phone, String password, String email,String role) {
        return ResultVo.success(tUserService.save(new TUser(phone,passwordEncoder.encode(password),email,role)));
    }
    @GetMapping("/check")
    public ResultVo<Boolean> checkphone(Long id,String field, String value) {
        return ResultVo.success(tUserService.check(id,field,value));
    }
    @PostMapping("/getByToken")
    public ResultVo<TUser> getByToken(String token) {
        return ResultVo.success(tUserService.getById((JwtUtil.getIdFromToken(token))));
    }

    @PostMapping("/update")
    public ResultVo<Boolean> update(Long id, String username, String nickName, Integer gender,Integer age, String phone, String email, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVo.success(tUserService.update(id, username, nickName,gender,age, phone, email, file));
    }

    @PostMapping("/sendVerificationCode")
    public ResultVo<Boolean> sendVerificationCode(String email) {
        return ResultVo.success(tUserService.sendVerificationCode(email));
    }

    @PostMapping("/verifyCode")
    public ResultVo<Boolean> verifyCode(String email, String code) {
        return ResultVo.success(tUserService.verifyCode(email, code));
    }

    @PostMapping("/resetPassword")
    public ResultVo<Boolean> resetPassword(String email, String newPassword) {
        return ResultVo.success(tUserService.resetPassword(email, newPassword));
    }

    @PostMapping("/del")
    ResultVo<Boolean> deleteTUser(Long id,Boolean status) {
        return ResultVo.success(tUserService.del(id,status),status ? "启用用户成功" : "禁用用户成功");
    }

    @PostMapping("/recharge")
    public ResultVo<Boolean> setProperty(Long id, BigDecimal value) {
        return ResultVo.success(tUserService.recharge(id, value));
    }

    @PostMapping("/transfer")
    public ResultVo<Boolean> transfer(Long id, String tophone, BigDecimal transferAmount) {
        return ResultVo.success(tUserService.transfer(id, tophone, transferAmount));
    }
    @PostMapping("/paycheck")
    public ResultVo<Boolean> paycheck(Long id, String password,BigDecimal paymoney) {
        return ResultVo.success(tUserService.payCheck(id, password,paymoney));
    }
//    @PostMapping("/payif")
//    public ResultVo<Boolean> profee(Long id, BigDecimal value) {
//        return ResultVo.success(tUserService.payif(id, value));
//    }

    @PostMapping("/Buslogin")
    public ResultVo<LoginVO> Buslogin(String phone, String password) {
        return ResultVo.success(tUserService.Buslogin(phone, password));
    }

    @PostMapping("/getById")
    public ResultVo<TUser> getById(Long id) {
        return ResultVo.success(tUserService.getById(id));
    }
}
